package org.desktop.web.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.desktop.web.client.role.RoleService;
import org.desktop.web.server.locator.ServiceLocator;
import org.desktop.web.server.locator.ServiceLocator.ServiceName;
import org.desktop.web.server.util.RpcUtil;
import org.desktop.web.shared.Response;
import org.desktop.web.shared.domain.entities.Role;

import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;

public class RoleServiceImpl extends AbstractPagingService<Role> implements
		RoleService {

	private String ROLES = RpcUtil.createUrlService(
			ServiceLocator.find(ServiceName.TIMESHEET_SERVICE), "roleses");

	@Override
	public Class<?> getType() {
		// TODO Auto-generated method stub
		return Role.class;
	}

	@Override
	public Response getRoles(String token) throws Exception {
		// TODO Auto-generated method stub
		return new Response() {
			{
				setResponseCode(000);
				setSuccess(true);
				setJsonMetadata("{\"warning\": \"not yet implemented\"}");
			}
		};
	}

	@Override
	public PagingLoadResult<Role> getRoles(final String token,
			PagingLoadConfig loadConfig) throws Exception {
		Map<String, String> headers = new HashMap<String, String>() {
			{
				put("token", token);
			}
		};
		// TODO Auto-generated method stub
		return getDatas(headers, loadConfig, ROLES);
	}

	@Override
	public boolean existsCodRole(Map<String, String> headers, String codRole)
			throws Exception {
		HttpURLConnection con = RpcUtil.getUrlConnection(new URL(ROLES
				+ "/exists/codRole/" + codRole), "GET", headers);
		InputStream in = con.getInputStream();

		String result = IOUtils.toString(in);
		in.close();

		return new Boolean(result).booleanValue();
	}

	@Override
	public Response saveRole(Map<String, String> headers, Role role)
			throws Exception {
		InputStream in = null;
		try {
			HttpURLConnection con = RpcUtil.getRESTConnection(new URL(ROLES),
					"POST", headers);
			RpcUtil.sendData(con, RpcUtil.objectToJson(role).getBytes());
			in = con.getInputStream();
			Response response = new Response();
			response.setResponseCode(con.getResponseCode());
			response.setSuccess(con.getResponseCode() == 201);
			// TODO Auto-generated method stub
			return response;
		} finally {
			if (in != null) {
				in.close();
			}
		}
	}

	@Override
	public Response deleteRole(Map<String, String> headers, Role selectedItem)
			throws Exception {
		InputStream in = null;
		try {
			HttpURLConnection con = RpcUtil.getRESTConnection(new URL(ROLES
					+ "/" + selectedItem.getId()), "DELETE", headers);
			in = con.getInputStream();
			Response response = new Response();
			response.setResponseCode(con.getResponseCode());
			return response;
		} finally {
			if (in != null) {
				in.close();
			}
		}
	}

}
